<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Material extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    public function add(){
        $user = $this->auth->getUser();
        $sex = $this->request->post('sex');
        $birth = $this->request->post('birth');
        $height = $this->request->post('height');
        $weight = $this->request->post('weight');
        $name = $this->request->post('name');
        $mobile = $this->request->post('mobile');
        if(!$sex || !$birth || !$height || !$weight || !$name || !$mobile){
            $this->error("缺少比传参数");
        }
        $data = Db::name("material")->where(['uid'=>$user->id])->find();
        if($data){//修改
            $in_data['sex'] = $sex;
            $in_data['birth'] = $birth;
            $in_data['height'] = $height;
            $in_data['weight'] = $weight;
            $in_data['name'] = $name;
            $in_data['mobile'] = $mobile;
            $in_data['updatetime'] = time();
            $in_status = Db::name("material")->where(['uid'=>$user->id])->update($in_data);
            if($in_status !== false){
                $this->success("成功");
            }else{
                $this->error("失败");
            }
        }else{//添加
            $in_data['uid'] = $user->id;
            $in_data['sex'] = $sex;
            $in_data['birth'] = $birth;
            $in_data['height'] = $height;
            $in_data['weight'] = $weight;
            $in_data['name'] = $name;
            $in_data['mobile'] = $mobile;
            $in_data['createtime'] = time();
            $in_data['updatetime'] = time();
            $in_status = Db::name("material")->insertGetId($in_data);
            if($in_status){
                $this->success("成功");
            }else{
                $this->error("失败");
            }
        }


    }
    public function details(){
        $user = $this->auth->getUser();
        $data = Db::name("material")->where(['uid'=>$user->id])->find();
        $this->success("成功",$data);
    }
}
